<?php

namespace app\ctrl;

use app\service\Predis;

class RedisCtrl extends \core\Base
{

    public function set()
    {
        $stock_key = $_GET['key'] ?? 'goods_stock_2';
        $stock_num = $_GET['num'] ? intval($_GET['num']) : 3;
        $ret = Predis::getInstance()->set($stock_key, $stock_num);
        if ($ret) {
            echo '初始化库存成功！';
        }
    }

    public function get()
    {
        $stock_key = $_GET['key'] ?? 'goods_stock_2';
        $num = Predis::getInstance()->get($stock_key);
        echo $num;
    }

    public function del()
    {
        $key = $_GET['key'] ?? 'goods_stock_2';
        $ret = Predis::getInstance()->del($key);
        if ($ret) {
            echo '删除成功！';
        }
    }

    /**
     * 延迟队列测试
     * @return mixed
     */
    public function delayQueue()
    {
        $dq = \app\service\RedisDelayQueue::getInstance('close_order');
        $time = time();
        $dq->addTask($time + 30, ['uid' => '1-30', 'order' => uniqid()]);
        $dq->addTask($time + 60, ['uid' => '1-60', 'order' => uniqid()]);
        $dq->addTask($time + 90, ['uid' => '2-90', 'order' => uniqid()]);

        while (true) {
            $dq->run();
            usleep(100000);
        }
    }

}